﻿namespace PMS.Client
{
    using DevComponents.DotNetBar;
    using PMS.BLL;
    using PMS.Client.Properties;
    using PMS.Common;
    using PMS.Enums;
    using PMS.Model;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmLogin : Office2007Form
    {
        private AdminBLL bll = new AdminBLL();
        private Button btnLogin;
        private CheckBox cbRememberPwd;
        private IContainer components = null;
        private int i = 0;
        private Label label1;
        private Label label2;
        private Label lbErrorPwd;
        private Label lbErrorUserName;
        private Admin model = null;
        private bool netError = false;
        private PictureBox pictureBox1;
        private Timer timer1;
        private TextBox txtPassword;
        private TextBox txtUsername;

        public FrmLogin()
        {
            this.InitializeComponent();
            this.lbErrorPwd.Text = string.Empty;
            this.lbErrorUserName.Text = string.Empty;
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                this.model = this.bll.Login(this.txtUsername.Text.Trim(), DESEncrypt.GetMd5(this.txtPassword.Text.Trim()));
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                this.netError = true;
            }
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (this.netError)
            {
                this.lbErrorPwd.Text = "网络连接失败";
                this.UpdateLoginBtn();
            }
            else if (this.model == null)
            {
                this.lbErrorPwd.Text = "密码不正确";
                this.UpdateLoginBtn();
            }
            else if (this.model.OutUseDate < DateTime.Now)
            {
                this.lbErrorPwd.Text = "帐号已过期";
                this.UpdateLoginBtn();
            }
            else if (!this.model.IsEnable)
            {
                this.lbErrorPwd.Text = "帐号被停用";
                this.UpdateLoginBtn();
            }
            else
            {
                this.cbRememberPwd_CheckedChanged(this, e);
                PMS.Common.Keys.Admin = this.model;
                SyslogBLL.Write(SysLogType.ADMIN_LOGIN, "用户" + this.model.LoginName + "登陆");
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
            this.btnLogin.Enabled = false;
            if (this.txtUsername.Text.Trim() == "")
            {
                this.lbErrorUserName.Text = "用户名不能为空";
                this.timer1.Enabled = false;
                this.btnLogin.Text = "登陆";
                this.btnLogin.Enabled = true;
            }
            else
            {
                this.lbErrorUserName.Text = "";
                if (this.txtPassword.Text.Trim() == "")
                {
                    this.lbErrorPwd.Text = "密码不能为空";
                    this.timer1.Enabled = false;
                    this.btnLogin.Text = "登陆";
                    this.btnLogin.Enabled = true;
                }
                else
                {
                    this.lbErrorPwd.Text = "";
                    BackgroundWorker worker = new BackgroundWorker();
                    worker.DoWork += new DoWorkEventHandler(this.bgWorker_DoWork);
                    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.bgWorker_RunWorkerCompleted);
                    worker.RunWorkerAsync();
                }
            }
        }

        private void cbRememberPwd_CheckedChanged(object sender, EventArgs e)
        {
            IniConfig.Write("USER", "ISREMEMBERPWD", this.cbRememberPwd.Checked.ToString());
            if (this.cbRememberPwd.Checked)
            {
                IniConfig.Write("USER", "USERNAME", DESEncrypt.Encrypt(this.txtUsername.Text));
                IniConfig.Write("USER", "PASSWORD", DESEncrypt.Encrypt(this.txtPassword.Text));
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.RememberPwd();
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmLogin));
            this.label1 = new Label();
            this.txtUsername = new TextBox();
            this.txtPassword = new TextBox();
            this.label2 = new Label();
            this.btnLogin = new Button();
            this.lbErrorUserName = new Label();
            this.lbErrorPwd = new Label();
            this.timer1 = new Timer(this.components);
            this.cbRememberPwd = new CheckBox();
            this.pictureBox1 = new PictureBox();
            ((ISupportInitialize) this.pictureBox1).BeginInit();
            base.SuspendLayout();
            this.label1.AutoSize = true;
            this.label1.Location = new Point(0x30, 0x77);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x35, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "用户名：";
            this.txtUsername.BorderStyle = BorderStyle.FixedSingle;
            this.txtUsername.Location = new Point(0x6b, 0x74);
            this.txtUsername.Name = "txtUsername";
            this.txtUsername.Size = new Size(0xa2, 0x15);
            this.txtUsername.TabIndex = 1;
            this.txtPassword.BorderStyle = BorderStyle.FixedSingle;
            this.txtPassword.Location = new Point(0x6b, 160);
            this.txtPassword.Name = "txtPassword";
            this.txtPassword.PasswordChar = '*';
            this.txtPassword.Size = new Size(0xa2, 0x15);
            this.txtPassword.TabIndex = 3;
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0x30, 0xa3);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x35, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "密  码：";
            this.btnLogin.FlatStyle = FlatStyle.System;
            this.btnLogin.Location = new Point(0x11e, 0x73);
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Size = new Size(70, 0x43);
            this.btnLogin.TabIndex = 4;
            this.btnLogin.Text = "登陆";
            this.btnLogin.UseVisualStyleBackColor = true;
            this.btnLogin.Click += new EventHandler(this.btnLogin_Click);
            this.lbErrorUserName.AutoSize = true;
            this.lbErrorUserName.ForeColor = Color.Red;
            this.lbErrorUserName.Location = new Point(0x69, 140);
            this.lbErrorUserName.Name = "lbErrorUserName";
            this.lbErrorUserName.Size = new Size(0x59, 12);
            this.lbErrorUserName.TabIndex = 6;
            this.lbErrorUserName.Text = "用户名不能为空";
            this.lbErrorPwd.AutoSize = true;
            this.lbErrorPwd.ForeColor = Color.Red;
            this.lbErrorPwd.Location = new Point(0x69, 0xb8);
            this.lbErrorPwd.Name = "lbErrorPwd";
            this.lbErrorPwd.Size = new Size(0x4d, 12);
            this.lbErrorPwd.TabIndex = 7;
            this.lbErrorPwd.Text = "密码不能为空";
            this.timer1.Interval = 500;
            this.timer1.Tick += new EventHandler(this.timer1_Tick);
            this.cbRememberPwd.AutoSize = true;
            this.cbRememberPwd.Location = new Point(0x6b, 0xc7);
            this.cbRememberPwd.Name = "cbRememberPwd";
            this.cbRememberPwd.Size = new Size(0x48, 0x10);
            this.cbRememberPwd.TabIndex = 8;
            this.cbRememberPwd.Text = "记住密码";
            this.cbRememberPwd.UseVisualStyleBackColor = true;
            this.pictureBox1.BackgroundImage = Resources.login_bg;
            this.pictureBox1.BackgroundImageLayout = ImageLayout.None;
            this.pictureBox1.Dock = DockStyle.Top;
            this.pictureBox1.Location = new Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new Size(0x18a, 90);
            this.pictureBox1.TabIndex = 5;
            this.pictureBox1.TabStop = false;
            base.AcceptButton = this.btnLogin;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xc2, 0xd9, 0xf7);
            base.ClientSize = new Size(0x18a, 0xe3);
            base.Controls.Add(this.cbRememberPwd);
            base.Controls.Add(this.lbErrorPwd);
            base.Controls.Add(this.lbErrorUserName);
            base.Controls.Add(this.pictureBox1);
            base.Controls.Add(this.btnLogin);
            base.Controls.Add(this.txtPassword);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.txtUsername);
            base.Controls.Add(this.label1);
            this.DoubleBuffered = true;
            base.EnableGlass = false;
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.KeyPreview = true;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmLogin";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "登陆";
            base.Load += new EventHandler(this.FrmLogin_Load);
            ((ISupportInitialize) this.pictureBox1).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void RememberPwd()
        {
            try
            {
                string str = IniConfig.Read("USER", "ISREMEMBERPWD");
                bool result = false;
                if (bool.TryParse(str, out result) && result)
                {
                    string text = IniConfig.Read("USER", "USERNAME");
                    string str3 = IniConfig.Read("USER", "PASSWORD");
                    this.txtUsername.Text = DESEncrypt.Decrypt(text);
                    this.txtPassword.Text = DESEncrypt.Decrypt(str3);
                    this.cbRememberPwd.Checked = true;
                }
                else
                {
                    this.cbRememberPwd.Checked = false;
                }
                this.cbRememberPwd.CheckedChanged += new EventHandler(this.cbRememberPwd_CheckedChanged);
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.i == 0)
            {
                this.btnLogin.Text = "登陆中   ";
            }
            else if (this.i == 1)
            {
                this.btnLogin.Text = "登陆中.  ";
            }
            else if (this.i == 2)
            {
                this.btnLogin.Text = "登陆中.. ";
            }
            else if (this.i == 3)
            {
                this.btnLogin.Text = "登陆中...";
                this.i = 0;
            }
            this.i++;
        }

        private void UpdateLoginBtn()
        {
            this.timer1.Enabled = false;
            this.btnLogin.Text = "登陆";
            this.btnLogin.Enabled = true;
        }
    }
}

